<?php
/**
 * Created by xuan.
 * Author: xuan
 * Date: 2018/8/3
 * Time: 18:24
 */

namespace app\api\controller\v1;

use app\api\model\Goods as GoodsModel;
use think\Exception;
use think\Log;
use think\Request;
use think\Session;
use app\common\service\GoodsSpecService;

class Goods
{
    
	
	/**
     * 前端-列表
     */
    public function homelists()
    {
		
		//$data = GoodsModel::GoodsLists();
		if (request()->isPost()){
			$length = 10;
			
			$all = Request::instance()->param();
			
			if(empty($all['start'])){ $start = 0; }else{ $start = trim($all['start']); }
			if(empty($all['draw'])){ $draw = 1; }else{ $draw = trim($all['draw']); }
			if(empty($all['goodsname'])){ $goodsname = ''; }else{ $goodsname = trim($all['goodsname']); }
			if(empty($all['isshow'])){ $isshow = 0; }else{ $isshow = trim($all['isshow']); }
			if(empty($all['catid'])){ $catid = 0; }else{ $catid = trim($all['catid']); }
			
			$isshow = 1;
			$where = ' 1 = 1 ';
            
			$time = time() - 86400;
			$where .= " AND ((is_time_limit = 1 and end_time >= $time) or (is_time_limit = 0))";
			
			$cons = [];
			if($goodsname){
			    $cons ['goodsname'] = ['like','%' . $goodsname . '%'];
			}
			
			if($catid){
			    $cons ['catid'] = $catid;
			}
			if($isshow){
			    $cons ['isshow'] = $isshow;
			}
			
			$list = GoodsModel::getListByWhere($where, $start, $length,$cons);
			$count = GoodsModel::getCount($where,$cons);

			foreach ($list as &$item){
			    unset($item['reward']);
			    unset($item['supply_price']);
			    $item['pic']= get_image($item['pic'],'!200');
			}
			$data = array("returnCode"=>1,"returnMsg"=>"success!", 'recordsTotal' => $count, 'recordsFiltered' => $count, 'draw' => $draw,'data' => $list);
			return json($data);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!","data"=>""]);
		}
    }
	
	/**
     * 删除一条
     */
    public function del($id)
    {
		if(empty($id)){
			return json(["returnCode"=>-6,"returnMsg"=>"必填参数不能为空!","data"=>""]);exit;
		}
		$dataone = GoodsModel::selectOneGoodsByID($id);
		if($dataone){
			$data = GoodsModel::delOneGoods($id);
			if($data){
				return json(["returnCode"=>1,"returnMsg"=>"success!"]);
			}else{
				return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
			}
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
    }
	
	/**查询一条记录*/
	public function find($id){
	
			
		$data = GoodsModel::selectOneGoodsByID($id);
		if($data){
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
	}
	
	/**查询一条记录*/
	public function homefind($id){
	
			
		$data = GoodsModel::selectOneGoodsByID($id);
		if($data){
		    $data = $data->toArray();
		    $multipic = explode(',', $data['multipic']);
		    $pics =[];
		    foreach ($multipic as &$pic){
		        $pics[] = get_image($pic,'!750x400');
		    }
		    $data['pics'] = $pics;
		    $keys = [
		        'supply_price','multipic','reward',
		    ];
		    foreach ($keys as $key){
		        unset($data[$key]);
		    }
		    $data['price'] =  sprintf("%.2f",$data['price']);
		    $data['detail'] = replace_article_images($data['detail']);
		    $data['need_attr'] = $data['goods_type_id'] > 0;
		    $data['attr_price'] = GoodsSpecService::getGoodsPriceMap($data);
		    
			return json(["returnCode"=>1,"returnMsg"=>"success!","data"=>$data]);
		}else{
			return json(["returnCode"=>-2,"returnMsg"=>"fail!"]);
		}
	}
	
	
}